package com.darren.framework.initial;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import com.darren.framework.component.SystemParameterHelper;
import com.darren.framework.utils.JWTUtil;
import com.darren.framework.utils.StringUtil;


/**
 * <pre>
 * *********************************************
 * Copyright Darren Zhou.
 * All rights reserved.
 * Description: token parameter initial 
 * HISTORY:
 * *********************************************
 *  Version		Date		Author	   		Desc	
 *   v1.0     2018-05-20    Darren Zhou    	Create
 * *********************************************
 * </pre>
 */

public class InitialJWTParameter implements InitializingBean,DisposableBean {	
	
	private Logger logger = LogManager.getLogger(InitialJWTParameter.class);

	public void afterPropertiesSet() throws Exception {
		logger.info("initial token parameter begin......");
		
		/**
		 * 初始化 access token 过期时间，0 表未永不过期
		 */
		JWTUtil.setAccessExpired(SystemParameterHelper.getAccessTokenExpire());
		logger.info("initial token parameter access token expire......" + SystemParameterHelper.getAccessTokenExpire());
				
		/**
		 * 初始化 access token 加密密钥
		 */
		if(!StringUtil.isEmptyOrWhitespace(SystemParameterHelper.getAccessTokenSecureSecret())){
			JWTUtil.setAccessSecret(SystemParameterHelper.getAccessTokenSecureSecret());
			logger.info("initial token parameter access token secure secret......" + SystemParameterHelper.getAccessTokenSecureSecret());
		}
		
		/**
		 * 初始化 refresh token 过期时间，0 表未永不过期
		 */
		JWTUtil.setRefreshExpired(SystemParameterHelper.getRefreshTokenExpire());
		logger.info("initial token parameter refresh token expire......" + SystemParameterHelper.getRefreshTokenExpire());
				
		/**
		 * 初始化 refresh token 加密密钥
		 */
		if(!StringUtil.isEmptyOrWhitespace(SystemParameterHelper.getRefreshTokenSecureSecret())){
			JWTUtil.setRefreshSecret(SystemParameterHelper.getRefreshTokenSecureSecret());
			logger.info("initial token parameter refresh token secure secret......" + SystemParameterHelper.getRefreshTokenSecureSecret());
		}
		
		/**
		 * 初始化  token 发行者
		 */
		if(!StringUtil.isEmptyOrWhitespace(SystemParameterHelper.getTokenIssuer())){
			JWTUtil.setIssuer(SystemParameterHelper.getTokenIssuer());
			logger.info("initial token parameter issuer......" + SystemParameterHelper.getTokenIssuer());
		}		
		
		logger.info("initial token parameter completed......");
	}

	public void destroy() throws Exception {
		
	}

}
